<?php

namespace App\Services\Utils;

use App\Http\Models\Product;
use App\Http\Models\Store;
use App\Http\Models\StoreSupplyPrice;

/**
 * kaka分成计算(benyamin的分成没有参与实际的运算中)
 * Class ProfitReckon
 * @package App\Services\Utils
 */
class ProfitReckon
{

    public static function customerProfit($price, $product, $store)
    {
        if ( self::checkCustomerDivided($product)===true )
        {
            return $price * (100 - $store->customer->division_precent - $store->division_proportion) / 100;
        }else{
            return $price * ($product->productable->customer_reserved_income/100);
        }
    }

    private static function checkCustomerDivided($product)
    {
        if ( $product->productable->customer_reserved_income=='' )
        {
            return true;
        }else{
            return false;
        }
    }

    public static function storeReservedIncome($store_id,$product_id,$product_type)
    {
        $store_supply_price = StoreSupplyPrice::where([
            ['store_id',$store_id],
            ['product_id',$product_id],
            ['product_type',$product_type]
        ])->first();

        if ( !$store_supply_price )
        {
            return Store::find($store_id)->division_proportion;
        }else{
            return $store_supply_price->reserved_income;
        }

    }

    public static function storeIncomeYield($store_id,$product_id,$product_type,$price)
    {
        return (self::storeReservedIncome($store_id,$product_id,$product_type)*$price)/100/100;

    }

    public static function clerkIncome($store_id,$product_id,$product_type,$price)
    {
        $product = Product::where([
            ['productable_id',$product_id],
            ['productable_type',$product_type],
        ])->first();

        $store = Store::find($store_id);

//        return self::customerProfit($price, $product, $store);
        return $price - (self::storeReservedIncome($store_id,$product_id,$product_type)/100*$price + self::customerProfit($price, $product, $store));

    }



}